package com.spa.interfaces.massagist;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.spa.application.command.massagist.*;
import com.spa.application.dto.PageResult;
import com.spa.application.dto.massagist.MassagistOrderDetailDTO;
import com.spa.application.dto.massagist.MassagistOrderListDTO;
import com.spa.application.query.massagist.MassagistOrderDetailQuery;
import com.spa.application.query.massagist.MassagistOrderQuery;
import com.spa.application.service.massagist.MassagistOrderApplicationService;
import com.spa.infrastructure.common.ResponseMessage;
import com.spa.interfaces.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.rmi.ServerException;

@RestController
@RequestMapping("massagist/order/")
@Slf4j
public class MassagistOrderController extends BaseController {

    @Autowired
    private MassagistOrderApplicationService messagistOrderApplicationService;

    @PostMapping("/query/my/order")
    public ResponseMessage<PageResult<MassagistOrderListDTO>> queryMyOrders(@RequestBody MassagistOrderQuery query) {
        return responseHelper.success(messagistOrderApplicationService.queryMyOrderByStatus(query));
    }

    @PostMapping("detail")
    public ResponseMessage<MassagistOrderDetailDTO> detail(@Validated @RequestBody MassagistOrderDetailQuery query) {
        return responseHelper.success(messagistOrderApplicationService.detail(query));
    }

    @PostMapping("/refresh/workplace")
    public ResponseMessage<Object> touchCall1(@Validated @RequestBody MassagistOrderWorkplaceCommand command) {
         messagistOrderApplicationService.workplace(command.getOrderId(),command.getWorkplace());
        return responseHelper.success();
    }

    @PostMapping("/cancelOrder")
    public ResponseMessage<Object> cancelOrder(@Validated @RequestBody MassagistOrderCancelCommand command) {
        messagistOrderApplicationService.cancelOrder(command.getOrderId());
        return responseHelper.success();
    }

    @PostMapping("receiveOrder")
    public ResponseMessage receiveOrder(@Validated @RequestBody MassagistOrderCommand command) throws ServerException {
        messagistOrderApplicationService.receiveOrder(command.getOrderId());
        return responseHelper.success();
    }

    @PostMapping("setOut")
    public ResponseMessage setOut(@Validated @RequestBody MassagistOrderCommand command) throws ServerException {
        messagistOrderApplicationService.setOut(command.getOrderId());
        return responseHelper.success();
    }

    @PostMapping("reach")
    public ResponseMessage reach(@Validated @RequestBody MassagistOrderCommand command) throws ServerException {
        messagistOrderApplicationService.reach(command.getOrderId());
        return responseHelper.success();
    }

    @PostMapping("startService")
    public ResponseMessage startService(@Validated @RequestBody MassagistOrderCommand command) throws ServerException {
        messagistOrderApplicationService.startService(command.getOrderId());
        return responseHelper.success();
    }

    @PostMapping("completeService")
    public ResponseMessage completeService(@Validated @RequestBody MassagistOrderCommand command) throws ServerException {
        messagistOrderApplicationService.completeService(command.getOrderId());
        return responseHelper.success();
    }

    @PostMapping("/submit/order/transfer")
    public ResponseMessage orderOperTransfer(@RequestBody MassagistOrderOperTransferCommand command) {
        Assert.isFalse( ObjectUtil.isNull(command.getOrderId()) || 0L == command.getOrderId() , "缺少订单关键信息");
        messagistOrderApplicationService.orderTransfer(command.getOrderId());
        return responseHelper.success();
    }

    @PostMapping("/callUser")
    public ResponseMessage<String> callUser(@Validated @RequestBody MassagistOrderCancelCommand command) throws Exception {
        return responseHelper.success(messagistOrderApplicationService.callUser(command.getOrderId()));
    }

}
